package cn.sx.config;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 前台拦截器
 *@Time: 2020/9/16  10:16
 */

import cn.sx.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IndexInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private TypesService typesService;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handle)throws Exception{
        request.setAttribute("typeList",typesService.findAll());//为所有页面设置类目列表
        //指定拦截路径
        String uri = request.getRequestURI();
        if (uri.contains("index/cart") || uri.contains("index/order") || uri.contains("index/my")) {
            Object user=request.getSession().getAttribute("user");
            if (user == null ) {
                response.sendRedirect("login");
                return false;
            }
        }
        //默认放过
        return true;
    }

}
